package model;

import java.text.*;
import java.util.*;

public class ProductService_ans {
	private ProductDAO dao = new ProductDAO();
	private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	
	public List<String[]> select(ProductBean bean) {
		List<String[]> result = null;
		
		if(bean!=null && bean.getId()!=0) {
			ProductBean tempBean = dao.select(bean.getId());
			if(tempBean!=null){
				String[] temp = new String[5];
				temp[0] = ""+tempBean.getId();
				temp[1] = tempBean.getName();
				temp[2] = ""+tempBean.getPrice();

				java.util.Date make = tempBean.getMake();
				if(make!=null) {
					temp[3] = sdf.format(make);					
				}
				temp[4] = ""+tempBean.getExpire();
				
				result = new ArrayList<String[]>();
				result.add(temp);
			}
			
		} else if(bean!=null && bean.getName()!=null && bean.getName().length()!=0) {
			result = dao.select(bean.getName());
		} else {
			result = dao.select();
		}
		return result;
	}
	public ProductBean insert(ProductBean bean) {
		ProductBean result = null;
		
		String name = bean.getName();
		if(name!=null && name.length()!=0) {
			result = dao.insert(bean);
		} else {
			result = dao.insert(bean.getId());
		}
		return result;
	}

	public ProductBean update(ProductBean bean) {
		return this.dao.update(bean);
	}
	public int delete(ProductBean bean) {
		return dao.delete(bean.getId());
	}
}
